<?php
require_once 'entity/Runner.php';
require_once 'entity/Team.php';
require_once 'tools/service/tools.php';
require_once 'tools/http/HTTP_REQUEST.php';
require_once 'tools/constant/Config.php';
require_once 'tools/db/queries.php';
include 'pages/header/correction_header.php';

$obj_utils=new DbUtils();
$tool=new Tool($dbUtils);
$obj_utils->connect();
// Load current course
$course=$obj_utils->getCourse();

if(HTTP_RESQUEST::exist("actionType") && HTTP_RESQUEST::post("actionType")=='changeResults'){

	$newHour=HTTP_RESQUEST::post("newHour");
	$newMin=HTTP_RESQUEST::post("newMin");
	$newSec=HTTP_RESQUEST::post("newSec");
	if(is_numeric($newHour) && is_numeric($newMin) && is_numeric($newSec)){
		$resultId=HTTP_RESQUEST::post("resultId");
		$course=HTTP_RESQUEST::post("course");
		$obj_utils->updateResultTime($resultId, $course,$newHour,$newMin,$newSec);
	}else{
		?>
		<script>alert("veuillez entrer une valeur numérique pour les champs ");</script>
		<?php 
	}

}else if(HTTP_RESQUEST::exist("actionType") && HTTP_RESQUEST::post("actionType")=='changeCourse'){

	$course=HTTP_RESQUEST::post("changeCourse");

}

?>
<form action="./index.php?page=correction/correction_results.php" method="post" id="changeCourseForm">
	<select name="changeCourse" onchange="document.getElementById('changeCourseForm').submit();">
		<?php foreach (Config::$courses as $currentCourse){ ?>
		<option value="<?php echo $currentCourse;?>" <?php echo $course==$currentCourse?"selected='selected'":""; ?>>
			<?php echo $currentCourse;?>
		</option>
		<?php } ?>
	</select>
	
	<input type="hidden" value="changeCourse" name="actionType"/>
</form>
<br>
<?php 

$query=getQueryForKey("results",$course);
$tabRunners=$obj_utils->executeQuery($query);

if($tabRunners!=0){?>
	<table class="principale">
			<tr>
				<th colspan="7"><h2>Correction des résultats de <?php echo $course;?></h2></th>
			</tr>
			<tr>
				<th>Général</th>
				<th>Formation</th>
				<th>Dossard</th>
				<th>Temps</th>
				<th>Nouveau Temps</th>
				<th></th>
			</tr>
		
			<?php
			if(sizeof($tabRunners)>0){
			$time=$obj_utils->getTime($course);
			
			for($i=0;$i<sizeof($tabRunners);$i++){?>
			<form  action="./index.php?page=correction/correction_results.php" method="post" id="updateTimeForm">
				<tr class="list" style="font-size: 12px">
					<td class="listing"><?php echo $i+1; ?></td>
					<td class="listing"><?php echo($tool->getFormation($tabRunners[$i])); ?></td>
					<td class="listing" align="center"><?php echo($tabRunners[$i]['dossard']); ?></td>
					<td class="listing" align="center"><?php echo($tool->getTimeDif($time,$tabRunners[$i]['temp'])); ?></td>
					<td class="listing" align="center">
						<span>
							<input type="text" name="newHour" size="3" maxlength="2" value="<?php echo($tool->getTimeDif($time,$tabRunners[$i]['temp'],Tool::HOUR)); ?>"/>
							<input type="text" name="newMin" size="3" maxlength="2" value="<?php echo($tool->getTimeDif($time,$tabRunners[$i]['temp'],Tool::MIN)); ?>"/>
							<input type="text" name="newSec" size="3" maxlength="2" value="<?php echo($tool->getTimeDif($time,$tabRunners[$i]['temp'],Tool::SEC)); ?>"/>
						</span>
					<td>
						<input type="hidden" value="<?php echo($tabRunners[$i]['resultId']); ?>" name="resultId"/>
						
						<input type="hidden" value="<?php echo $course;?>" name="course"/>
						<input type="hidden" value="changeResults" name="actionType"/>
						
						<input type="submit" value="Mise à jour du temps d'arrivée" />
					</td>
					
				</tr>
			</form>
			<?php }
			} ?>
		</table>
<?php 
}

$obj_utils->close();
?>